<!DOCTYPE html>
<html >
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="layui-form" id="edit_task_form" lay-filter="edit_task_form"  style="padding: 20px 0 0 0;"  >

    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label">任务名称</label>
            <div class="layui-input-inline">
                <input type="text" id = "taskName" maxlength="10" lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">任务内容</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入描述信息" id="content" name="content" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">任务积分</label>
            <div class="layui-input-inline">
                <input type="text" id = "integral" maxlength="30"  placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>上传附件</legend>
    </fieldset>

    <!--<div class="layui-upload">
        <button type="button" class="layui-btn" id="test2">图片上传</button>
        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
            预览图：
            <div class="layui-upload-list" id="demo2"></div>
        </blockquote>
        <button type="button" class="layui-btn" id="test3">上传</button>

    </div>-->
    <div class="layui-upload">
        <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
        <div class="layui-upload-list" style="max-width: 1000px;">
            <table class="layui-table" id="del_table" lay-filter="del_table">
                <colgroup>
                    <col>
                    <col width="150">
                    <col width="260">
                    <col width="150">
                </colgroup>
                <thead>
                <tr>
                    <th>文件名</th>
                    <th>大小</th>
                    <th>上传进度</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="demoList">

                </tbody>
            </table>
        </div>
        <button type="button" class="layui-btn" id="testListAction">开始上传</button>
    </div>

    <div class="layui-form-item" style="display:none;">
        <button class="layui-btn" id="submit_task" lay-submit="" lay-filter="submit_task"></button>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
<!--            <label class="layui-form-label">任务积分</label>-->
            <div class="layui-input-inline">
                <input type="hidden" name="hiddenFilePath" id="hiddenFilePath" value="">
            </div>
        </div>
    </div>
</div>
<script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"/>

    <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
<script type="text/javascript">
    layui.use(['upload', 'element', 'layer', 'table'], function() {
        var $ = layui.jquery
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer
            , table = layui.table;
        var uploadListIns = upload.render({
            elem: '#testList'
            ,elemList: $('#demoList') //列表元素对象
            ,url: '/task/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            ,accept: 'file'
            ,multiple: true
            ,number: 3
            ,auto: false
            ,bindAction: '#testListAction'
            ,choose: function(obj){
                debugger
                var that = this;
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function(index, file, result){
                    var tr = $(['<tr id="upload-'+ index +'">'
                        ,'<td><img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img"></td>'
                        ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                        ,'<td><div class="layui-progress" lay-filter="progress-demo-'+ index +'"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    that.elemList.append(tr);
                    element.render('progress'); //渲染新加的进度条组件
                });
            }
            ,done: function(res, index, upload){//成功的回调
                var filePath = $("#hiddenFilePath").val();
                if (filePath == '') {
                    $("#hiddenFilePath").val(res.data);
                } else {
                    $("#hiddenFilePath").val(filePath+"," + res.data);
                }
                alert(filePath);
                var that = this;
                //if(res.code == 0){ //上传成功
                var tr = that.elemList.find('tr#upload-'+ index)
                    ,tds = tr.children();
                // tds.eq(3).html(''); //清空操作
                delete this.files[index]; //删除文件队列已经上传成功的文件
                return;
                //}
                this.error(index, upload);
            }
            ,allDone: function(obj){ //多文件上传完毕后的状态回调
                console.log(obj)
            }
            ,error: function(index, upload){ //错误回调
                var that = this;
                var tr = that.elemList.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
            ,progress: function(n, elem, e, index){ //注意：index 参数为 layui 2.6.6 新增
                element.progress('progress-demo-'+ index, n + '%'); //执行进度条。n 即为返回的进度百分比
            }
        });

            //监听表格复选框选择
           /* table.on('tool', function(obj){
                console.log(obj)
            });*/
            //监听工具条
            table.on('tool(del_table)', function(obj){
                var data = obj.data;
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr;
                alert(JSON.stringify(data));
                if(layEvent === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        obj.del();
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    layer.alert('编辑行：<br>'+ JSON.stringify(data))
                }
            });

            window.deletepic=function (obj,id) {
                var parentNode = obj.parentNode.parentNode.parentNode;
                let parentNode1 = obj.parentNode.parentNode;
                parentNode.removeChild(parentNode1);
                $.ajax({
                    type: "POST",
                    contentType: "application/json",//设置请求参数类型为json字符串
                    url: "/task/delFile/"+id,
                    success : function(result) {
                    },
                    error : function() {
                        alert("删除错误!");
                    }
                });
            }
    });

    /*function deletepic(obj,id) {
        alert(id);
        var parentNode = obj.parentNode.parentNode.parentNode;
        let parentNode1 = obj.parentNode.parentNode;
        parentNode.removeChild(parentNode1);
        $.ajax({
            type: "POST",
            contentType: "application/json",//设置请求参数类型为json字符串
            url: "/task/delFile/"+id,
            success : function(result) {
            },
            error : function() {
                alert("删除错误!");
            }
        });
    }*/
</script>
</body>





